home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / PrintWWJr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  7.4 KB  |  344 lines  |  [TEXT/KAHL]

  1. /* PrintWWJr.c
  2.  * Handle printing in Writeswell Jr.
  3.  *
  4.  * ©1992 Working Software, Inc.
  5.  * This source code is copyrighted.  Permission is granted to use the Word Services
  6.  * portion of the Writeswell Jr. source code in your own programs, but you 
  7.  * may not distribute the Writeswell Jr. word-processor code as a 
  8.  * commercial product.  If you modify the code, please do not call it 
  9.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  10.  * program and don’t have to deal with a number of different versions with 
  11.  * who-knows-what going on in the code.
  12.  * 
  13.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  14.  *
  15.  * 17 Oct 92 Mike Crawford - The third anniversary of the Loma Prieta Earthquake.
  16.  *                            Downtown Santa Cruz is still under reconstruction.
  17.  */
  18.  
  19. #include <EPPC.h>
  20. #include <AppleEvents.h>
  21. #include <AEObjects.h>
  22. #include <Printing.h>
  23. #include "AERegistry.h"
  24. #include "WordServices.h"
  25. #include "TestBed.h"
  26. #include "TBConstants.h"
  27. #include "Gripe.h"
  28. #include "Scroll.h"
  29. #include "Prefs.h"
  30. #include "TBGlobals.h"
  31. #include "PrintWWJr.h"
  32. #include "UnloadStuff.h"
  33. #include "PageSetup.h"
  34. #include "TTPictures.h"
  35.  
  36. void PostPrintingErrors( OSErr theErr );
  37. void PrintPage( Rect rPage, short pageNumber, Boolean *morePages );
  38. void MakePrinterText( THPrint thePrRecHdl );
  39. void SetPrintRect( Rect *rPagePtr );
  40. void SetWindowRect( void );
  41.  
  42. static Rect    gOldDestRect;
  43. static Rect gOldViewRect;
  44.  
  45. void DoPrint( void )
  46. {
  47.     GrafPtr        oldPort;
  48.     short        copies;
  49.     short        firstPage;
  50.     short        lastPage;
  51.     short        numberOfCopies;
  52.     short        printmgrsResFile;
  53.     short        realNumberOfPagesInDoc;
  54.     short        pageNumber;
  55.     short        printError;
  56.     THPrint        thePrRecHdl;
  57.     TPPrPort    thePrPort;
  58.     TPrStatus    theStatus;
  59.     Rect        rPage;
  60.     Boolean        morePages;
  61.  
  62.     GetPort( &oldPort );
  63.  
  64.     thePrRecHdl = GetPrintRecord();
  65.  
  66.     if ( !thePrRecHdl ){
  67.         Gripe( "\pCannot get print record" );
  68.         return;
  69.     }
  70.  
  71.     UnloadForPrint();
  72.         
  73.     PrOpen();
  74.     
  75.     if ( PrError() == noErr ){
  76.     
  77.         printmgrsResFile = CurResFile();
  78.         
  79.         if ( PrJobDialog( thePrRecHdl ) ){
  80.         
  81.             numberOfCopies = (**thePrRecHdl).prJob.iCopies;
  82.             firstPage = (**thePrRecHdl).prJob.iFstPage;
  83.             lastPage = (**thePrRecHdl).prJob.iLstPage;
  84.                     
  85.             /* STUB show printing status dialog */
  86.             
  87.             for ( copies = 1; copies <= numberOfCopies; copies++ ){
  88.             
  89.                 /* STUB install idle proc */
  90.                 
  91.                 /* Restore the resource file to the printer driver */
  92.                 
  93.                 UseResFile( printmgrsResFile );
  94.                 
  95.                 thePrPort = PrOpenDoc( thePrRecHdl,
  96.                                         (TPPrPort)NULL,
  97.                                         (Ptr)NULL );
  98.                 
  99.                 pageNumber = firstPage;
  100.                 
  101.                 rPage = (**thePrRecHdl).prInfo.rPage;
  102.                 
  103.                 SetPrintRect( &rPage );        /* Set TE Rec to fit on printer paper */
  104.  
  105.                 morePages = true;
  106.  
  107.                 while ( pageNumber <= lastPage && morePages && PrError() == noErr ){
  108.                     PicHandle    thePict;
  109.                     GrafPtr        curPort;
  110.                     
  111.                     GetPort( &curPort );
  112.                     SetPort( gDocWindow );
  113.                 
  114.                     thePict = OpenPicture( &rPage );
  115.                     
  116.                     /*ClipRect( &(thePort->portRect) );*/
  117.                     /*ClipRect( &rPage );*/
  118.                     
  119.                     if ( !thePict ){
  120.                         PrSetError( memFullErr );
  121.                     }else{
  122.                         PrintPage( rPage, pageNumber, &morePages );
  123.                         ClosePicture();
  124.                         
  125.                         SetPort( curPort );
  126.  
  127.                         PrOpenPage( thePrPort, (TPRect)NULL );
  128.                         
  129.                         if ( PrError() == noErr ){
  130.                             DrawPicture( thePict, &rPage );
  131.                         }
  132.                         
  133.                         PrClosePage( thePrPort );
  134.                         
  135.                         DisposeHandle( (Handle) thePict );
  136.                     }
  137.                     pageNumber++;
  138.                 }/* while pageNumber */
  139.                 
  140.                 SetWindowRect();            /* Restore TE Rec to fit in window */
  141.  
  142.                 PrCloseDoc( thePrPort );
  143.  
  144.                 if (!PrError() &&
  145.                         ((*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop)){
  146.  
  147.                     PrPicFile( thePrRecHdl, 
  148.                                 (TPrPort*)NULL, 
  149.                                 (Ptr)NULL, 
  150.                                 (Ptr)NULL, 
  151.                                 &theStatus);
  152.                 }
  153.  
  154.  
  155.             }/* for copies */
  156.         }else{
  157.             /* The print job is being canceled by the user
  158.              * from the job dialog
  159.              */
  160.             PrSetError( iPrAbort );
  161.         }
  162.     }/* If PrError */
  163.     
  164.     printError = PrError();
  165.     
  166.     PrClose();
  167.     
  168.     /* 1.0.2 Check for iPrAbort - user canceled Job dialog or pressed Cmd Dot */
  169.  
  170.     if ( printError != noErr && printError != iPrAbort )
  171.         PostPrintingErrors( printError );
  172.     
  173.     ReleaseResource( (Handle) thePrRecHdl );
  174.     
  175.     /* STUB dispose of progress dialog */
  176.  
  177.     SetPort( oldPort );
  178.  
  179.     return;
  180. }
  181.  
  182. void PostPrintingErrors( OSErr theErr )
  183. {
  184.     /* STUB Explain what the error really was */
  185.     
  186.     Gripe( "\pAn error occurred while printing" );
  187.  
  188.     return;
  189. }
  190.  
  191. void SetPrintRect( Rect *rPagePtr )
  192. {
  193.     TEHandle    textH;
  194.  
  195.     /* Resize the TextEdit record's dest and view rects to fit the printer paper */
  196.  
  197.     textH = (TEHandle)GetWRefCon( gDocWindow );
  198.     
  199.     gOldDestRect = (*textH)->destRect;
  200.     gOldViewRect = (*textH)->viewRect;
  201.     
  202.     (*textH)->destRect = *rPagePtr;
  203.     (*textH)->viewRect = *rPagePtr;
  204.  
  205.     TECalText( textH );
  206.  
  207.     return;
  208. }
  209.  
  210. void SetWindowRect( void )
  211. {
  212.     TEHandle    textH;
  213.     GrafPtr        curPort;
  214.     
  215.     /* Restore the TE rec's original view and dest rects */
  216.  
  217.     GetPort( &curPort );
  218.     SetPort( gDocWindow );
  219.  
  220.     textH = (TEHandle)GetWRefCon( gDocWindow );
  221.     
  222.     (*textH)->destRect = gOldDestRect;
  223.     (*textH)->viewRect = gOldViewRect;
  224.  
  225.     TECalText( textH );
  226.  
  227.     /* Force a redraw of the text */
  228.     InvalRect( &gOldViewRect );
  229.     
  230.     SetPort( curPort );
  231.  
  232.     return;
  233. }
  234.  
  235. void PrintPage( Rect rPage, short pageNumber, Boolean *morePages )
  236. {
  237.     TEHandle    textH;
  238.     Rect        clipRect;
  239.     short        docHeight;
  240.     short        pageHeight;
  241.     Rect        viewRect;
  242.     Rect        destRect;
  243.  
  244.     textH = (TEHandle)GetWRefCon( gDocWindow );
  245.  
  246.     docHeight = TEGetHeight( 1, (*textH)->nLines, textH );
  247.     
  248.     pageHeight = rPage.bottom - rPage.top;
  249.     
  250.     if ( pageHeight * pageNumber > docHeight ){
  251.         *morePages = false;
  252.     }else{
  253.         *morePages = true;
  254.     }
  255.  
  256.     SetRect( &clipRect, 0, 0, 0, 0 );
  257.     ClipRect( &clipRect );
  258.     
  259.     viewRect = (*textH)->viewRect;
  260.     destRect = (*textH)->destRect;
  261.  
  262.     TEScroll( 0, viewRect.top - destRect.top, textH );
  263.     TEScroll( 0, - ( pageHeight * (pageNumber - 1) ), textH );
  264.  
  265.     clipRect = rPage;
  266.  
  267. /*
  268.     clipRect.top += (pageHeight * (pageNumber - 1) );    
  269.     clipRect.bottom += (pageHeight * (pageNumber - 1) );
  270. */
  271.  
  272.     ClipRect( &clipRect );
  273.  
  274.     TEUpdate( &rPage, textH );
  275.  
  276.     /* 1.1 MDC display teachtext-style pictures */
  277.  
  278.     ShowPictures( gDocWindow, gNumPictures, gResRefNum );
  279.     
  280.     return;
  281. }
  282.  
  283. #ifdef NEVER
  284. void MakePrinterText( THPrint thePrRecHdl )
  285. {
  286.     Rect            destRect;
  287.     Rect            viewRect;
  288.     CharsHandle        textHdl;
  289.     TEStyleHandle    stylHdl;
  290.     TEHandle        winTEHdl;
  291.     long            textLen;
  292.     short            oldStart;
  293.     short            oldEnd;
  294.  
  295.     /* If there are any errors, we post them into the printer manager, so
  296.      * that upon return, the error is checked and we will exit the printing loop.
  297.      * I think that should work properly.
  298.      *
  299.      * It will have the advantage that the printing loop will be entirely exited
  300.      * before the error is actually displayed.
  301.      */
  302.  
  303.     destRect = (*thePrRecHdl)->prInfo.rPage;
  304.     viewRect = destRect;
  305.  
  306.     gPrintTextHdl = TEStylNew( &destRect, &viewRect );
  307.     
  308.     if ( !gPrintTextHdl ){
  309.         PrSetError( memFullErr );
  310.         return;
  311.     }
  312.  
  313. #ifdef NEVER    
  314.     /* Copy the text from the window's TE rec to the printer's TE rec */
  315.     
  316.     winTEHdl = (TEHandle)GetWRefCon( gDocWindow );
  317.  
  318.     textHdl = TEGetText( winTEHdl );
  319.     
  320.     textLen = GetHandleSize( textHdl );
  321.     
  322.     HLock( textHdl );
  323.     TEInsert( *textHdl, textLen, gPrintTextHdl );
  324.     HUnlock( textHdl );
  325.     
  326.     /* Copy the styles from the window's TE rec to the printer's */
  327.  
  328.     oldStart = (*winTEHdl)->selStart;
  329.     oldEnd = (*winTEHdl)->selEnd;
  330.     (*winTEHdl)->selStart = 0;
  331.     (*winTEHdl)->selEnd = 32767;
  332.  
  333.     stylHdl = GetStylHandle( winTEHdl );
  334.  
  335.     (*winTEHdl)->selStart = oldStart;
  336.     (*winTEHdl)->selEnd = oldEnd;
  337.     
  338.     SetStylHandle( stylHdl, gPrintTextHdl );
  339. #endif
  340.  
  341.     return;
  342. }
  343. #endif
  344.